// pages/my/putforward.js
import Router from '../../utils/router.js'
import base from '../../config/base.js'
import util from '../../utils/util.js'
import service from '../../services/phonenumber';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isphone: 0,
    user_info: {},
    balance: '0.00',
    phone: '',
    count_balance: ''
  },
  isPhone: function () {
    let that = this,
      user_info = wx.getStorageSync('user_info'),
      isphone = 0;

    if (user_info.phone) {
      isphone = 1;
    }
    that.setData({
      isphone,
      user_info,
    });
  },
  getphone: function (e) {
    let that = this,
      auth_token = wx.getStorageSync('auth_token');
    if (e.detail.iv && e.detail.encryptedData) {
      // 提交手机信息 成功后跳转页面
      let param = {
        encrypted_data: e.detail.encryptedData,
        iv: e.detail.iv,
        auth_token: auth_token
      }
      service.phonenumber(param, function (res) {
        if (res && res.code == 1) {
          let phone = res.data.phone;
          let user_info = wx.getStorageSync('user_info');
          user_info.phone = phone;
          wx.setStorageSync('user_info', user_info);
          that.setData({
            user_info: user_info,
            isphone: 1
          })
          that.goregphone();

        }
      });
    } else {
      wx.showToast({
        title: '为了保证您的资金安全，需要校验手机号码。',
        icon: 'none',
        duration: 5000
      });
    }
  },
  goregphone: function () {
    let that = this;
    let data = that.data;
    if (data.balance >= 3 && data.count_balance <= 0 && data.balance <= 10000) {
      Router.go('putphone', `phone=${that.data.user_info.phone}`);
    } else if (data.count_balance > 0) {
      wx.showToast({
        title: '每日最多可提现1次',
        icon: 'none'
      })
    } else if (data.balance < 3) {
      wx.showToast({
        title: '单次提现最低额度为3元',
        icon: 'none'
      })
    } else if (data.balance > 10000){
      wx.showToast({
        title: '提现额度最高10000元',
        icon: 'none',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      balance: options.balance,
      count_balance: options.count_balance
    });


    this.isPhone();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分亨
   */
  // onShareAppMessage: function () {

  // }
})